package com.cskaoyan.service.back;

import com.cskaoyan.bean.bo.RoleDeleteBO;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.back.*;
import com.cskaoyan.mapper.MarketPermissionInfoMapper;
import com.cskaoyan.mapper.MarketPermissionMapper;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author 12-江涛
 * @since 2022/10/12  19:24
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    MarketRoleMapper marketRoleMapper;
    @Autowired
    MarketPermissionInfoMapper permissionInfoMapper;
    @Autowired
    MarketPermissionMapper permissionMapper;

    @Override
    public CommonData<RoleOptionsVO> roleOptions() {

        MarketRoleExample marketRoleExample = new MarketRoleExample();

        //查数量
        long total = marketRoleMapper.countByExample(marketRoleExample);
        //查内容
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);

        List<RoleOptionsVO> list = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {

            RoleOptionsVO roleOptionsVO = new RoleOptionsVO();
            roleOptionsVO.setValue(marketRole.getId());
            roleOptionsVO.setLabel(marketRole.getName());
            list.add(roleOptionsVO);
        }

        CommonData<RoleOptionsVO> commonDataVO = new CommonData<>();
        commonDataVO.setList(list);
        commonDataVO.setTotal((int) total);
        commonDataVO.setLimit((int) total);
        commonDataVO.setPage(1);
        commonDataVO.setPages(1);

        return commonDataVO;
    }

    @Override
    public CommonData getRoleList(BasePageInfo basePageInfo, String name) {
        CommonData<MarketRole> commonData = new CommonData<>();
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());
        marketRoleExample.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        if (name != null) {
            MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
            criteria.andNameLike("%" + name + "%");
        }
        long roleCount = marketRoleMapper.countByExample(marketRoleExample);
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);
        PageInfo<MarketRole> marketRolePageInfo = new PageInfo<>(marketRoles);

        commonData.setTotal((int) roleCount);
        commonData.setPages(marketRolePageInfo.getPages());
        commonData.setLimit(basePageInfo.getLimit());
        commonData.setPage(basePageInfo.getPage());
        commonData.setList(marketRolePageInfo.getList());
        return commonData;
    }

    @Override
    public RoleCreateVO CreateRole(String name, String desc) {
        RoleCreateVO roleCreateVO = new RoleCreateVO();
        Date date = new Date();
        MarketRole marketRole = new MarketRole(
                null,
                name,
                desc,
                true,
                date,
                date,
                false
        );
        int effected = marketRoleMapper.insertSelective(marketRole);
        if (effected == 1) {
            RoleCreateVO roleCreateVO2 = new RoleCreateVO(
                    marketRole.getId(),
                    marketRole.getName(),
                    marketRole.getDesc(),
                    marketRole.getEnabled(),
                    marketRole.getAddTime(),
                    marketRole.getUpdateTime(),
                    marketRole.getDeleted()
            );
            return roleCreateVO2;
        }
        return roleCreateVO;
    }

    @Override
    public int DeleteRole(RoleDeleteBO roleDeleteBO) {
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        MarketRoleExample.Criteria criteria = marketRoleExample.createCriteria();
        criteria.andEnabledEqualTo(true);
        criteria.andDeletedEqualTo(false);
        criteria.andIdEqualTo(roleDeleteBO.getId());
        int effected = marketRoleMapper.deleteByExample(marketRoleExample);
        return effected;
    }

    @Override
    public int UpadateRole(MarketRole marketRole) {
        int effected = marketRoleMapper.updateByPrimaryKeySelective(marketRole);
        return effected;
    }

    @Override
    public AdminRolePermissionsVo getRolePermission(Integer roleId) {
        // 查询对应roleId的授权
        List<String> permissions = permissionMapper.selectPermissionByRoleIdAndDeleted(roleId,false);
        // 根据权限之间的关系，查询所有的权限信息
        List<AdminRoleSystemPermissionsVo> systemPermissions = permissionInfoMapper.selectPermissionsByRealation();
        AdminRolePermissionsVo rolePermissionsVo = new AdminRolePermissionsVo();
        rolePermissionsVo.setSystemPermissions(systemPermissions);
        rolePermissionsVo.setAssignedPermissions(permissions);
        return rolePermissionsVo;

    }

    @Override
    public String getRoleName(Integer roleId) {
        MarketRole marketRole = marketRoleMapper.selectByPrimaryKey(roleId);
        String name = marketRole.getName();
        return name;
    }

    @Override
    public List<String> getRolePermissionByRoleId(Integer roleId) {
        List<String> list = permissionMapper.selectPermissionByRoleIdAndDeleted(roleId, false);
        return list;
    }

    @Override
    public InfoData queryRoleInfo() {
        // 获得此管理员的信息
        Subject subject = SecurityUtils.getSubject();
        MarketAdmin primaryPrincipal = (MarketAdmin) subject.getPrincipals().getPrimaryPrincipal();
        // 根据此管理员的角色id查询出所有的角色信息
        Integer[] roleIds = primaryPrincipal.getRoleIds();
        List<Integer> roleIdList = Arrays.asList(roleIds);
        MarketRoleExample roleExample = new MarketRoleExample();
        roleExample.createCriteria().andIdIn(roleIdList);
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(roleExample);
        List<String> roles = new ArrayList<>();
        Iterator<MarketRole> iterator = marketRoles.iterator();
        while (iterator.hasNext()) {
            MarketRole marketRole = iterator.next();
            roles.add(marketRole.getName());
        }
        // 根据角色信息的id查询所有的授权
        MarketPermissionExample permissionExample = new MarketPermissionExample();
        permissionExample.createCriteria().andRoleIdIn(roleIdList);
        List<MarketPermission> marketPermissions = permissionMapper.selectByExample(permissionExample);
        ArrayList<String> perms = new ArrayList<>();
        List<PessionInfoVO> apiList=permissionMapper.selectApi();
        // Iterator<MarketPermission> permissionIterator = marketPermissions.iterator();
        for (PessionInfoVO pessionInfoVO : apiList) {
            for (MarketPermission permission : marketPermissions) {
                if (pessionInfoVO.getId().equals(permission.getPermission())){
                    perms.add(pessionInfoVO.getApi());
                }
            }
            // while (permissionIterator.hasNext()) {
            //         MarketPermission permission = permissionIterator.next();
            // }
        }
        InfoData infoData = new InfoData();
        infoData.setName(primaryPrincipal.getUsername());
        infoData.setAvatar(primaryPrincipal.getAvatar());
        infoData.setRoles(roles);
        infoData.setPerms(perms);
        return infoData;
    }

}
